/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.sys.service.impl.SysUserServiceImpl.java 
 * <p>
 * 类产生时间: 2014/4/26 0026 下午 12:53
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.service.impl;

import cn.com.oceansoft.nos.common.constant.Constant;
import cn.com.oceansoft.nos.common.util.MD5Util;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.persistence.ISysUserMapper;
import cn.com.oceansoft.business.sys.service.ISysUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 系统用户业务操作组件实现类
 *
 * @author: chenw
 * @time: 2014/4/26 0026 下午 12:53
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private ISysUserMapper sysUserMapper;

    @Override
    public Pager<SysUser> findByPage(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<SysUser> page = (Page<SysUser>) sysUserMapper.findByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    @Transactional
    public Result save(SysUser sysUser) {
        sysUser.setId(UUID.randomUUID().toString());
        int rt = sysUserMapper.insert(sysUser);
        if (rt > 0) {
            sysUser.setPassword(Constant.DEFAULT_USER_PASSWORD);
            return new Result(sysUser);
        } else {
            return new Result(Result.FAILURE, "用户新增失败");
        }
    }

    @Override
    @Transactional
    public Result update(SysUser sysUser) {
        sysUserMapper.update(sysUser);
        return new Result();
    }

    @Override
    public SysUser findUserById(String userId) {
        return sysUserMapper.findById(userId);
    }

    @Override
    @Transactional
    public Result delete(String userId) {
        sysUserMapper.delete(userId);
        sysUserMapper.deleteUserRole(userId);
        return new Result();
    }

    @Override
    @Transactional
    public void updateUserRoleBatch(final String userId, final int[] ids) {
        Map<String, Object> param = new HashMap<String, Object>() {{
            put("userId", userId);
            put("roleIds", ids);
        }};
        //删除指定ID的角色对应的资源信息
        sysUserMapper.deleteUserRoleRelationByUserId(userId);
        //批量插入关联信息
        sysUserMapper.saveUserRoleRelationByBatch(param);
    }

    @Override
    public Result loginAuthentication(SysUser sysUser) {
        try {
            sysUser.setPassword(MD5Util.getMD5String(sysUser.getPassword()));
//            sysUser.setPassword(DesJLUtils.encrypt(sysUser.getPassword()));
        }catch (Exception e){
            return new Result(Result.FAILURE, "密码加密失败");
        }

        SysUser db_user = sysUserMapper.findUserByLoginIdAndPwd(sysUser);
        if (null == db_user) {
            return new Result(Result.FAILURE, "用户名或密码错误");
        } else {
            if (SysUser.STATUS_INVALID == db_user.getStatus()) {
                return new Result(Result.FAILURE, "该账号已禁用，请与管理员联系");
            } else {
                db_user.setPassword("");
                return new Result(Result.SUCC, db_user);
            }
        }
    }

    @Override
    @Transactional
    public Result updateUserAccountInfo(SysUser user) {
        if (null == user) {
            return new Result(Result.FAILURE, "更新失败");
        }
        String _old = user.getOldPwd();
        String _new = user.getPassword();
        if (StringUtils.hasText(_new) && StringUtils.hasText(_old)) {
            user.setPassword(MD5Util.getMD5String(_old));
            SysUser db_user = sysUserMapper.findUserByLoginIdAndPwd(user);
            if (db_user != null) {
                user.setPassword(MD5Util.getMD5String(_new));
                sysUserMapper.update(user);
                return new Result();
            } else {
                return new Result(Result.FAILURE, "原始密码错误");
            }
        }
        return new Result(Result.FAILURE, "原始密码或新密码不能为空");
    }
}
